package ntd.util.frame.haikang.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.haikang.controller.bo.HaiKangCustomerInBO;
import ntd.util.frame.haikang.controller.bo.HaiKangGetStreamUrlInBO;
import ntd.util.frame.haikang.controller.bo.HaiKangIotInBO;
import ntd.util.frame.haikang.controller.bo.HaiKangOldGetStreamUrlInBO;
import ntd.util.frame.haikang.remote.HaiKangConfig;
import ntd.util.frame.haikang.remote.RemoteHaiKang;
import ntd.util.modular.function.entity.ChannelBusiness;
import ntd.util.modular.function.service.ChannelBusinessService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = {"公共能力-海康能力封装"})
@RestController
@RequestMapping("/frame/hai-kang")
@Valid
public class ControllerHaiKang {

    @Resource
    private ChannelBusinessService channelBusinessService;

    @GetMapping("page-bmunicipal")
    @ApiOperation("海康-物联网平台-井盖信息-分页查询")
    public Object pageBmunicipal(@Valid HaiKangIotInBO haiKangIotInBO) {
        RemoteHaiKang remoteHaiKang = initHaiKang(haiKangIotInBO.getChannelBusinessId());
        return remoteHaiKang.pageBmunicipal();
    }

    @GetMapping("customer-do-post")
    @ApiOperation("海康-自定义接口")
    public Object customerDoPost(@Valid HaiKangCustomerInBO haiKangCustomerInBO) {
        RemoteHaiKang remoteHaiKang = initHaiKang(haiKangCustomerInBO.getChannelBusinessId());
        return remoteHaiKang.doPost(haiKangCustomerInBO.getUri(), haiKangCustomerInBO.getJsonParams());
    }

    @PostMapping("customer-do-post")
    @ApiOperation("海康-自定义接口")
    public Object customerDoPostOfPost(@RequestBody HaiKangCustomerInBO haiKangCustomerInBO) {
        RemoteHaiKang remoteHaiKang = initHaiKang(haiKangCustomerInBO.getChannelBusinessId());
        return remoteHaiKang.doPost(haiKangCustomerInBO.getUri(), haiKangCustomerInBO.getJsonParams());
    }

    @GetMapping("get-stream-url")
    @ApiOperation("海康-获取取流链接")
    public Object getStreamUrlByCameraIndexCode(@Valid HaiKangGetStreamUrlInBO haiKangGetStreamUrlInBO) {
        RemoteHaiKang remoteHaiKang = initHaiKang(haiKangGetStreamUrlInBO.getChannelBusinessId());
        return remoteHaiKang.getStreamUrlByCameraIndexCode(
            haiKangGetStreamUrlInBO.getCameraIndexCode(), haiKangGetStreamUrlInBO.getStreamUrlProtocol().getProtocolName());
    }

    /**
     * 3a58b990274945aab868f798e66138e9
     * 40289f5a7e3c994d017e3c998daf0000
     * 2022-02-14T00:00:00.000+08:00
     * 2022-02-14T00:00:00.000+08:00
     * 2022-02-14T10:00:00.000+08:00
     * @param haiKangOldGetStreamUrlInBO 入参对象
     * @return 返回数据对象
     */
    @GetMapping("get-old-stream-url")
    @ApiOperation("海康-获取历史取流链接")
    public Object getOldStreamUrlByCameraIndexCode(@Valid HaiKangOldGetStreamUrlInBO haiKangOldGetStreamUrlInBO) {
        RemoteHaiKang remoteHaiKang = initHaiKang(haiKangOldGetStreamUrlInBO.getChannelBusinessId());
        return remoteHaiKang.getOldStreamUrlByCameraIndexCode(
            haiKangOldGetStreamUrlInBO.getCameraIndexCode(), haiKangOldGetStreamUrlInBO.getStreamUrlProtocol().getProtocolName(),
                haiKangOldGetStreamUrlInBO.getBeginTime(), haiKangOldGetStreamUrlInBO.getEndTime());
    }

    private RemoteHaiKang initHaiKang(String channelBusinessId) {
        ChannelBusiness channelBusiness = channelBusinessService.getById(channelBusinessId);
        if (ObjectUtil.isNull(channelBusiness)) {
            throw new ComException("找不到渠道商信息");
        }
        JSONObject settingJson = JSONUtil.parseObj(channelBusiness.getSettingJson());
        HaiKangConfig haiKangConfig = new HaiKangConfig();
        haiKangConfig.setHost(settingJson.getStr("host"));
        haiKangConfig.setAppKey(channelBusiness.getAppKey());
        haiKangConfig.setAppSecret(channelBusiness.getAppSecret());
        haiKangConfig.setHttpsTag(settingJson.getBool("httpsTag", false));
        return RemoteHaiKang.me(haiKangConfig);
    }
}
